package com.jk.kdd.system.service;

import com.jk.kdd.system.dao.SystemConstantMapper;
import com.jk.kdd.system.model.SystemConstant;
import com.jk.kdd.system.vo.SystemConstantVO;
import org.smart4j.framework.helper.BeanHelper;
import org.smart4j.framework.util.CastUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by DELL on 2016/10/9.
 */
@Service("systemConstantService")
public class SystemConstantServiceImpl implements  SystemConstantService {

    @Resource
    private SystemConstantMapper systemConstantMapper;

    /**
     * 获取常量
     */
    public List<SystemConstantVO> getSystemConstantByConstType(String constType) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("constType", constType);
        List<SystemConstant> systemConstant = systemConstantMapper.selectByCondition(paramMap);
        List<SystemConstantVO> tagVOList = BeanHelper.mapList(systemConstant,SystemConstantVO.class);
        return tagVOList;
    }

    /**
     * 获取常量
     */
    public List<SystemConstantVO> getSystemConstantByCondition(String constType, String parentKey) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("constType",constType);
        paramMap.put("parentKey", CastUtil.castLong(parentKey));
        List<SystemConstant> systemConstant = systemConstantMapper.selectByCondition(paramMap);
        List<SystemConstantVO> tagVOList = BeanHelper.mapList(systemConstant,SystemConstantVO.class);
        return tagVOList;
    }

    /**
     * 获取国家 区域 省 城市信息
     */
    public List<Map<String,Object>> getCountryInfo() {
        return getCountryInfoListByParentId(-1);
    }

    // 递归查询
    private List<Map<String,Object>> getCountryInfoListByParentId(int parentId ) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("parentId",parentId);
        List<Map<String,Object>> infoList = systemConstantMapper.selectCountryInfoByCondition(paramMap);

        for (int i = 0; i < infoList.size(); i++) {
            infoList.get(i).put("childList",getCountryInfoListByParentId(CastUtil.castInt(infoList.get(i).get("id"))));
        }

        return infoList;
    }
}
